/*
 * Particle.h
 *
 *  Created on: 2011-04-02
 *      Author: Admin
 */

#ifndef PARTICLE_H_
#define PARTICLE_H_

class Particle
{
public:
	Particle()
	{

	}

	~Particle()
	{

	}

	void generateParams(void)
	{
		lifetime = Math::Rand() % 3000;
		creationTime = 0;
		life = true;

		dir[0] = (Osp::Graphics::Opengl::GLfloat)(Math::Rand() % 100);
		dir[1] = (Osp::Graphics::Opengl::GLfloat)(Math::Rand() % 100);

		if(dir[0] >= 50)
		{
			dir[0] = 0.0005f * (100.0f - dir[0]);
		}
		else
		{
			dir[0] = -0.0005f * dir[0];
		}

		if(dir[1] >= 50)
		{
			dir[1] = 0.0005f * (100.0f - dir[1]);
		}
		else
		{
			dir[1] = -0.0005f * dir[1];
		}
	}

	void Update(long long ticks)
	{
		float fract = (float)(ticks - creationTime) / (float)lifetime;

		if(fract > 1.0f) fract = 1.0f;

		offsetX += dir[0] * (1.5f - fract);
		offsetY += dir[1] * (1.5f - fract);
		//AppLog("fract=%f | x=%f | y=%f | d[0]=%f | d[1]=%f", fract, offsetX, offsetY,dir[0],dir[1]);
	}

	void Reset(void)
	{
		dir[0] = 0;
		dir[1] = 0;
		offsetX = 0;
		offsetY = 0;
		lifetime = 0;
		creationTime = 0;
	}

	void setLifeTime(long long ticks)
	{
		if(creationTime == 0)
		{
			creationTime = ticks;
			return;
		}
		else
		{
			if(ticks - creationTime >= (long long)lifetime)
			{
				life = false;
			}
		}
	}

public:
	Osp::Graphics::Opengl::GLfloat x;
	Osp::Graphics::Opengl::GLfloat y;
	Osp::Graphics::Opengl::GLfloat width;
	Osp::Graphics::Opengl::GLfloat height;

	Osp::Graphics::Opengl::GLfloat vertArray[8];
	Osp::Graphics::Opengl::GLfloat texCoords[8];

	Osp::Graphics::Opengl::GLfloat dir[2];
	Osp::Graphics::Opengl::GLfloat offsetX;
	Osp::Graphics::Opengl::GLfloat offsetY;

	long long lifetime;
	long long creationTime;
	bool life;
};

#endif /* PARTICLE_H_ */
